package com.jilian.mydemo.modul.shop.ui;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;

import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseEventMsg;
import com.jilian.mydemo.base.BaseFragment;
import com.jilian.mydemo.common.dto.ProductDetailDto;
import com.jilian.mydemo.event.RxBus;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;


public class GoodsDetailCenterFragment extends BaseFragment {
    private WebView webview;




    @Override
    protected void createViewModel() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_goodsdetailcenter;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        webview = (WebView)view. findViewById(R.id.webview);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void initData() {


    }

    /**
     * 加载html标签
     *
     * @param bodyHTML
     * @return
     */
    private String getHtmlData(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                "<style>img{max-width: 100%; width:auto; height:auto!important;}</style>" +
                "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    @Override
    protected void initListener() {
        RxBus.getInstance().toObservable().map(new Function<Object, BaseEventMsg>() {
            @Override
            public BaseEventMsg apply(Object o) throws Exception {
                return (BaseEventMsg) o;
            }
        }).subscribe(new Consumer<BaseEventMsg>() {
            @Override
            public void accept(BaseEventMsg dto) throws Exception {
                if (dto != null&&dto.getProductDetailDto()!=null) {
                    //content是后台返回的h5标签
                    webview.loadDataWithBaseURL(null,
                            getHtmlData(dto.getProductDetailDto().getProductDetail()), "text/html", "utf-8", null);
                }
            }
        });
    }
}
